package com.firefly.admin.common.handle;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import com.firefly.admin.common.utils.DateUtil;
import com.firefly.admin.common.utils.JwtTokenUtil;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MybatisColumnsHandler implements MetaObjectHandler {
    private static final String FIELD_NAME_CREATE_TIME = "createTime";
    private static final String FIELD_NAME_CREATE_ID = "createUserId";
    private static final String FIELD_NAME_OPERATE_ID = "operateId";
    private static final String FIELD_NAME_OPERATE_TIME = "operateTime";

    @Override
    public void insertFill(MetaObject metaObject) {
        Date nowDate = DateUtil.getNowDate();
        this.strictInsertFill(metaObject, FIELD_NAME_CREATE_ID, Long.class, JwtTokenUtil.getLoginUserId());
        this.strictInsertFill(metaObject, FIELD_NAME_CREATE_TIME, Date.class, nowDate);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, FIELD_NAME_OPERATE_ID, Long.class, JwtTokenUtil.getLoginUserId());
        this.strictUpdateFill(metaObject, FIELD_NAME_OPERATE_TIME, Date.class, DateUtil.getNowDate());
    }
}
